package com.xiaobaibai.security.token.handler;

import com.xiaobaibai.units.TokenUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 为了安全,Token验证成功后我们就刷新一个Token响应回去
 * todo  但是vue-admin-template 那么好像没适配,先不管了
 */
@Slf4j
public class TokenSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        log.info("JWT验证成功处理层,todo 要根据时间更新token");
        //检验token是否快过期了,快过期了就更换一下
        String token = request.getHeader("X-Token");
        String newToken = TokenUnit.checkingUpdateToken(token, authentication);
        if(newToken!=null){//不为空就更新token
            response.setHeader("New-Token",newToken);
        }
    }

}
